//
//  LYVedioLiveController+Category.m
//  LYLiveApp
//
//  Created by QD on 16/9/3.
//  Copyright © 2016年 览益信息科技. All rights reserved.
//

#import "LYVedioLiveController+Category.h"

UIKIT_STATIC_INLINE void ScreenRotateToPortrait(UIInterfaceOrientation orientation) {
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = orientation;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}


@implementation LYVedioLiveController (Category)
#pragma mark ----- VHMoviePlayerDelegate
/**
 *  播放连接成功
 */
- (void)connectSucceed:(VHMoviePlayer*)moviePlayer info:(NSDictionary*)info {
    LYLog(@"播放连接成功");
    self.isNoVideoData = NO;
    [self.noVideoView removeFromSuperview];
    [self videoLiveBegin];
    [LYUtil hideHUDTo:self.moviePlayer.moviePlayerView];
    [self.indicator stopAnimating];
    [self.indicator removeFromSuperview];

}

/**
 *  缓冲开始回调
 */
- (void)bufferStart:(VHMoviePlayer*)moviePlayer info:(NSDictionary*)info {
    LYLog(@"缓冲开始回调");
    [self.moviePlayer.moviePlayerView addSubview:self.indicator];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

/**
 *  缓冲结束回调
 */
-(void)bufferStop:(VHMoviePlayer*)moviePlayer info:(NSDictionary*)info {
    LYLog(@"缓冲结束回调");
    [LYUtil hideHUDTo:self.moviePlayer.moviePlayerView];
    [self.indicator stopAnimating];
    [self.indicator removeFromSuperview];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

}
/**
 *  下载速率的回调
 *
 *  @param moviePlayer
 *  @param info        下载速率信息 单位kbps
 */
- (void)downloadSpeed:(VHMoviePlayer*)moviePlayer info:(NSDictionary*)info {
    //    LYLog(@"下载速率的回调");
    
    [LYUtil hideHUDTo:self.moviePlayer.moviePlayerView];
    [self.indicator stopAnimating];
    [self.indicator removeFromSuperview];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

}

/**
 *  网络状态的回调
 *
 *  @param moviePlayer
 *  @param info        网络状态信息  content的值越大表示网络越好
 */
- (void)netWorkStatus:(VHMoviePlayer*)moviePlayer info:(NSDictionary*)info {
    LYLog(@"网络状态的回调");
    
    LYLog(@"netWorkStatus:%f",[info[@"content"]floatValue]);
}
/**
 *  cdn 发生切换时的回调
 *
 *  @param moviePlayer
 *  @param info
 */
- (void)cdnSwitch:(VHMoviePlayer*)moviePlayer info:(NSDictionary*)info {
    LYLog(@"发生切换时的回调");
//    [LYUtil showProgressHUDAddedTo:self.moviePlayer.moviePlayerView];
    [self.moviePlayer.moviePlayerView addSubview:self.indicator];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

/**
 *  播放时错误的回调
 *
 *  @param livePlayErrorType 解盘错误类型
 */
- (void)playError:(LivePlayErrorType)livePlayErrorType info:(NSDictionary*)info {
    NSString * msg = @"";
    void (^resetStartPlay)(NSString * msg) = ^(NSString * msg){
        LYLog(@"msg -----  %@",msg);
    };
    
    switch (livePlayErrorType) {
        case kLivePlayParamError:
        {
            msg = @"参数错误";
            resetStartPlay(msg);
            [self _handleErrorAction];
        }
            break;
        case kLivePlayRecvError:
        {
            msg = @"对方已经停止解盘";
            resetStartPlay(msg);
            [self _handleErrorAction];
        }
            break;
        case kLivePlayCDNConnectError:
        {
            msg = @"服务器任性...连接失败";
            resetStartPlay(msg);
            [self _handleErrorAction];
        }
            break;
        case kLivePlayGetUrlError:
        {
            msg = @"获取服务器地址报错";
            resetStartPlay(msg);
            LYLog(@"error  ------%@",info[@"content"]);
            [self _handleErrorAction];
        }
            break;
        default:
            break;
    }
}

/**
 *  code	   含义
 *  10030	身份验证出错
 *  10402	当前活动ID错误
 *  10404	KEY值验证出错
 *  10046	当前活动已结束
 *  10047	您已被踢出，请联系活动组织者
 *  10048	活动现场太火爆，已超过人数上限
 */

#pragma mark -----VHallMoviePlayerDelegate
/**
 *  包含文档 获取翻页图片路径
 *
 *  @param changeImage  图片更新
 */
- (void)PPTScrollNextPagechangeImagePath:(NSString*)changeImagePath {
    
}
/**
 *  获取视频播放模式
 *
 *  @param playMode  视频播放模式
 */
- (void)VideoPlayMode:(VHallMovieVideoPlayMode)playMode {
    LYLog(@"model %ld",(long)playMode);
    
}
/**
 *  获取视频活动状态
 *
 *  @param playMode  视频活动状态
 */
- (void)ActiveState : (VHallMovieActiveState)activeState {
    switch (activeState) {
        case VHallMovieActiveStateLive:
            LYLog(@"解盘");
            break;
        case VHallMovieActiveStateReservation:
            LYLog(@"预约");
            break;
        case VHallMovieActiveStateEnd:
            LYLog(@"结束");
            break;
        case VHallMovieActiveStateReplay:
            LYLog(@"回放");
            break;
        default:
            break;
    }
    
}

/**
 *  该解盘支持的清晰度列表
 *
 *  @param definitionList  支持的清晰度列表
 */
- (void)VideoDefinitionList: (NSArray*)definitionList {
    LYLog(@"definitionList %@",definitionList);
    self.definitionArray = [NSArray arrayWithArray:definitionList];
}

/**
 *  解盘结束消息 如果视频正在播放 等下一次loading 解盘即结束
 *
 *  解盘结束消息
 */
- (void)LiveStoped {
    LYLog(@"解盘结束");
    ScreenRotateToPortrait(UIInterfaceOrientationPortrait);
    [self.moviePlayer stopPlay];
    [self videoLiveFinished];
//    [self _addVideoTimer];     
}

- (void)_handleErrorAction {
    WS(weakSelf)
  
    [[SensorsAnalyticsSDK sharedInstance] track:@"LivingWatch"
                                 withProperties:@{
                                                  @"ly_living_type" :[LYUtil lySensorsString:[LYLocalData sharedInstance].ly_living_type],
                                                  @"ly_living_definition":[LYUtil lySensorsString:[LYLocalData sharedInstance].ly_living_definition],
                                                  @"ly_living_succeed":@"成功",
                                                  @"ly_living_change_time":[LYUtil lySensorsString:[self _returnCurrentTime]],
                                                  @"ly_living_name":[LYUtil lySensorsString:[LYLocalData sharedInstance].roomInfo.name],
                                                  }];

    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf.moviePlayer stopPlay];
        [LYUtil hideHUDTo:weakSelf.moviePlayer.moviePlayerView];
        [weakSelf.indicator stopAnimating];
        [weakSelf.indicator removeFromSuperview];
        [weakSelf.moviePlayer.moviePlayerView addSubview:weakSelf.noVideoView];
        weakSelf.noVideoView.center = weakSelf.moviePlayer.moviePlayerView.center;
        weakSelf.moviePlayer.moviePlayerView.backgroundColor = RGB(155, 155, 155);
        weakSelf.isNoVideoData = YES;
        weakSelf.fullOverView.screenType = FullScreenTypeFail;
    });
    
}

- (NSString *)_returnCurrentTime {
    
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    NSString *dateTime = [formatter stringFromDate:date];
    return dateTime;
}

#pragma mark ----- 屏幕旋转     
-(BOOL)shouldAutorotate {
    if (self.isLock)  return NO;
    return YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return  UIInterfaceOrientationMaskAll;
}
@end
